﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using BTv2.Model;
using BTv2.Model.Actions;

namespace BTv2.Controls
{
   public class RulesController
    {
       private static RulesController _instance;
       private DatabaseController _dbinstance;
       private ObservableCollection<Rule> _rules;
       private ObservableCollection<Model.Action> _actions = new ObservableCollection<Model.Action>() { new TestAction() };
       public Dictionary<string, int> DailyExecutionCountDict = new Dictionary<string, int> { { "Every 5 min", 288 }, { "Every 10 min", 144 }, { "Every 15 min", 96 }, { "Every 20 min", 72 }, { "Every 30 min", 48 }, { "Every hour", 24 }, { "Every 2 hours", 12 }, { "Every 3 hours", 8 }, { "Every 6 hours", 4 }, { "Every 8 hours", 3 }, { "Every 12 hours", 2 }, { "Once a day", 1 } };


       public ObservableCollection<Rule> Rules
       {
           get { return _rules; }
       }

       public ObservableCollection<Model.Action> Actions
       {
           get { return _actions; }
       }

       public static RulesController GetInstance()
       {
           if (_instance == null) _instance = new RulesController();
           return _instance;
       }

       public ObservableCollection<Model.Action> GetActions(List<int> actionIDs)
       {
           return new ObservableCollection<Model.Action>(_actions.Where(a => actionIDs.Contains(a.ID)));
       }

       public async Task<bool> PreloadRules()
       {
           try
           {
               _rules = await _dbinstance.GetRules();
               return true;
           }
           catch
           {
               return false;
           }
       }

       public async Task<bool> DeleteRule(Rule r)
       {
           try
           {
               await _dbinstance.RemoveRule(r);
               try
               {

               Application.Current.Dispatcher.Invoke(() => Rules.Remove(r));
               }
               catch
               {
                   return true;
               }
               return true;
           }
           catch
           {
               return false;
           }
       }

       public async Task<bool> SaveRule(Rule r)
       {
           try
           {

               return await _dbinstance.SaveRule(r);
           }
           catch
           {
               return false;
           }
       }

       public async Task<bool> AddRule(Rule r)
       {
           try
           {
               bool res1 = await _dbinstance.AddRule(r);
               if (!res1) return res1;
               int id = await _dbinstance.GetLastRuleID();
               r.ID = id;
               return res1;
           }
           catch
           {
               return false;
           }
       }

       public Model.Action GetActions(int actionIDs)
       {
           return _actions.Where(a => a.ID == actionIDs).First();
       }

       public async void SaveRuleResult(Rule r, bool result)
       {
           try
           {
              await _dbinstance.SaveRuleResult(r.ID, result);
           }
           catch
           {

           }
       }

       public RulesController()
       {
           _dbinstance = DatabaseController.GetInstance();
       }

       public async Task<ObservableCollection<Model.Action>> GetFilteredActionList(Rule r)
        {
            return new ObservableCollection<Model.Action>(Actions.Except(r.Actions));
        }

    }
}
